package com.qf.flash.ui.chat;

import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.utils.ChatUserCacheMgr;
import com.hyphenate.exceptions.HyphenateException;
import com.qf.dblib.dao.entity.ChatUser;
import com.qf.dblib.dao.entity.GroupChatBean;
import com.qf.dblib.dao.impl.GroupDao;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.http.HttpManager;
import com.qf.flashlib.util.RxUtils;
import com.qf.flashlib.widget.dialog.CommonDialog;

import java.util.List;

import io.reactivex.disposables.Disposable;

//  对话窗口
public class MyChatFragment extends EaseChatFragment {
    private Disposable tipDisposable;

    @Override
    protected void setUpView() {
        super.setUpView();
//        titleBar.setRightImageResource(R.mipmap.nav_more);

        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (chatType == EaseConstant.CHATTYPE_SINGLE) {
//                    emptyHistory();
                } else {

                    toChatUsername = fragmentArgs.getString(EaseConstant.EXTRA_USER_ID);
                    GroupSetsActivity.start(getActivity(), toChatUsername);
                }
            }
        });
        GroupChatBean last = GroupDao.getInstance().findLast(UserMgr.getInstance().getLoginUser().getUid(), toChatUsername);

        if (chatType == EaseConstant.CHATTYPE_GROUP && last == null) {
            tipDisposable = HttpManager.getInstance().getGsonHttpApi().CHAT_TIPS()
                    .compose(RxUtils.handleResult())
                    .compose(RxUtils.io_main())
                    .subscribe(groupTipBean -> {
                        showTips(groupTipBean.getTip());
                    }, throwable -> {

                    });
        }
    }

    private void showTips(String tips) {
        GroupChatBean bean = new GroupChatBean();
        bean.setGroupID(toChatUsername);
        bean.setUid(UserMgr.getInstance().getLoginUser().getUid());
        bean.setTime(System.currentTimeMillis());
        GroupDao.getInstance().save(bean);
        CommonDialog.newInstance().setLayoutId(R.layout.dialog_tips)
                .setConvertListener((holder, dialog) -> {
                    holder.setText(R.id.tv_title, "群组聊天规范");
                    holder.setText(R.id.tv_content, tips);
                    TextView tvContnet = holder.getView(R.id.tv_content);
                    tvContnet.setMovementMethod(ScrollingMovementMethod.getInstance());
                    holder.setTextColor(R.id.tv_content, Color.RED);
                    holder.setOnClickListener(R.id.tv_left_btn, v -> {
                        dialog.dismiss();
                        FragmentActivity activity = getActivity();
                        if (activity != null)
                            activity.finish();
                    });
                    holder.setOnClickListener(R.id.tv_right_btn, v -> dialog.dismiss());
                }).setSize(300, 0)
                .show(getChildFragmentManager());
    }

    @Override
    public void onMessageReceived(List<EMMessage> messages) {
        super.onMessageReceived(messages);
        for (EMMessage message : messages) {
            try {
                String uid = message.getStringAttribute(FlashCodes.CHAT_UID);
                String pic = message.getStringAttribute(FlashCodes.CHAT_PIC_AVATAR);
                String nickName = message.getStringAttribute(FlashCodes.CHAT_NICK_NAME);
                String picFrame = message.getStringAttribute(FlashCodes.CHAT_PIC_FRAME);
                ChatUser chatUser = ChatUserCacheMgr.getInstance().find(uid);
                chatUser.setUid(uid);
                chatUser.setAvatar(pic);
                chatUser.setNick(nickName);
                chatUser.setPicFrame(picFrame);
                ChatUserCacheMgr.getInstance().saveUser(chatUser);
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (tipDisposable != null)
            tipDisposable.dispose();
    }

    @Override
    public void onBackPressed() {
//        HomeActivity.start(getActivity(),2);
        super.onBackPressed();
    }
}
